package com.atguigu.test.boot.properties;

import com.atguigu.test.boot.annotation.MyConfigurationProperties;
import com.atguigu.test.config.MyBatisAutoConfig;
import lombok.Data;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import java.lang.reflect.Field;

/**
 * @author zp
 * @version 1.0
 * @description: mysql属性配置类
 * @date 2023/2/7 19:38
 */
@MyConfigurationProperties(pre = "jdbc.")
public class MySqlProperties implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    private String username;
    private String password;
    private String url;
    private String driver;
    private String registerAliasesPackageUrl;
    private String addMappersPackageUrl;
    private String mybatisName = MyBatisAutoConfig.MY_BATIS_NAME;
    /**
     * @description:  初始化完成属性绑定 读取Environment 当中的值绑定到参数类当中
     * @author zp
     * @date: 2023/2/10 19:13
     */
    public void init() throws IllegalAccessException {
        String pre = MySqlProperties.class.getAnnotation(MyConfigurationProperties.class).pre();
        Field[] fields = MySqlProperties.class.getDeclaredFields();
        Environment environment = applicationContext.getEnvironment();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName().toLowerCase();
            field.set(this,environment.getProperty(pre+fieldName));
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getRegisterAliasesPackageUrl() {
        return registerAliasesPackageUrl;
    }

    public void setRegisterAliasesPackageUrl(String registerAliasesPackageUrl) {
        this.registerAliasesPackageUrl = registerAliasesPackageUrl;
    }

    public String getAddMappersPackageUrl() {
        return addMappersPackageUrl;
    }

    public void setAddMappersPackageUrl(String addMappersPackageUrl) {
        this.addMappersPackageUrl = addMappersPackageUrl;
    }

    public String getMybatisName() {
        return mybatisName;
    }

    public void setMybatisName(String mybatisName) {
        this.mybatisName = mybatisName;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}
